package com.survey.application.action;

import java.io.InputStream;
import java.io.StringBufferInputStream;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.interceptor.ParameterAware;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.survey.application.bean.User;
import com.survey.application.service.UserService;
@Controller
@Scope("prototype")
public class UserAction extends ActionSupport implements ModelDriven<User>,RequestAware,ParameterAware,SessionAware{
	private User user=new User();
	//从页面获得model  后期可以封装到父类
	@Override
	public User getModel() {
		return user;
	}
	private Map<String,Object> request;
	@Resource
	private UserService userService;
	/**
	 * 注册
	 * @return
	 */
	public String register(){
		return add();
	}
	public String add(){
		System.out.println();
		return "add";
	}
	public String save(){
		userService.saveUser(user);
		return "save";
	}
	private String loginFailure;
	public String getLoginFailure() {
		return loginFailure;
	}
	public void setLoginFailure(String loginFailure) {
		this.loginFailure = loginFailure;
	}
	/**
	 * 登陆
	 * @return
	 */
	public String login(){
		User loginUser= userService.checkLogin(user);
		if(loginUser==null){
			loginFailure="登陆失败";
			request.put("loginFailure", loginFailure);
			return "failure";
		}
		session.put("user", loginUser);
		return "login-success";
	}
	private InputStream inputStream;
	private String exist;
	/**
	 * 查看相同账号
	 * @return
	 */
	public String checkSameAccount(){
		String account=parameters.get("account")[0];
		//如果为true 则存在
		exist=userService.checkSameAccount(account);
		inputStream=new StringBufferInputStream(exist);
		return SUCCESS;
	}
	public String view(){
		user=(User) session.get("user");
		return "view";
	}
	public void getResponseText(String text){
		inputStream=new StringBufferInputStream(text);
	}
	@Override
	public void setRequest(Map<String, Object> request) {
		this.request=request;
	}
	private Map<String,String[]> parameters;
	@Override
	public void setParameters(Map<String, String[]> parameters) {
		this.parameters=parameters;
	}
	public InputStream getInputStream() {
		return inputStream;
	}
	public void setInputStream(InputStream inputStream) {
		this.inputStream = inputStream;
	}
	public String getExist() {
		return exist;
	}
	public void setExist(String exist) {
		this.exist = exist;
	}
	private Map<String,Object> session;
	@Override
	public void setSession(Map<String, Object> session) {
		this.session=session;
	}
}

